home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / uci.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.5 KB  |  193 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4.  
  5. class StateMachine:
  6.     '''
  7.     '''
  8.     STATE_IDLE = 'IDLE'
  9.     STATE_CONNECTING = 'CONNECTING'
  10.     options = None
  11.     __positionCommand = 'position startpos'
  12.     __haveMoves = False
  13.     buffer = ''
  14.     __readyToConfigure = False
  15.     __options = None
  16.     __ready = False
  17.     __inCallback = False
  18.     __queuedCommands = None
  19.     
  20.     def __init__(self):
  21.         '''
  22.         '''
  23.         self.options = { }
  24.         self._StateMachine__queuedCommands = []
  25.  
  26.     
  27.     def logText(self, text, style):
  28.         '''
  29.         '''
  30.         pass
  31.  
  32.     
  33.     def onOutgoingData(self, data):
  34.         '''
  35.         '''
  36.         pass
  37.  
  38.     
  39.     def onMove(self, move):
  40.         """Called when the AI makes a move.
  41.         
  42.         'move' is the move the AI has decided to make (string).
  43.         """
  44.         print 'UCI move: ' + move
  45.  
  46.     
  47.     def registerIncomingData(self, data):
  48.         '''
  49.         '''
  50.         self._StateMachine__inCallback = True
  51.         self.buffer += data
  52.         while True:
  53.             index = self.buffer.find('\n')
  54.             if index < 0:
  55.                 break
  56.             
  57.             line = self.buffer[:index]
  58.             self.buffer = self.buffer[index + 1:]
  59.             self.parseLine(line)
  60.         self._StateMachine__inCallback = False
  61.         if self._StateMachine__options is not None and self._StateMachine__readyToConfigure:
  62.             options = self._StateMachine__options
  63.             self._StateMachine__options = None
  64.             self.configure(options)
  65.         
  66.         if len(self._StateMachine__queuedCommands) > 0 and self._StateMachine__ready:
  67.             commands = self._StateMachine__queuedCommands
  68.             self._StateMachine__queuedCommands = []
  69.             for c in commands:
  70.                 self._StateMachine__sendCommand(c)
  71.             
  72.         
  73.  
  74.     
  75.     def __sendCommand(self, command):
  76.         '''
  77.         '''
  78.         if self._StateMachine__ready and not (self._StateMachine__inCallback):
  79.             self.onOutgoingData(command + '\n')
  80.         else:
  81.             self._StateMachine__queuedCommands.append(command)
  82.  
  83.     
  84.     def start(self):
  85.         '''
  86.         '''
  87.         self.onOutgoingData('uci\n')
  88.  
  89.     
  90.     def startGame(self):
  91.         '''
  92.         '''
  93.         self._StateMachine__sendCommand('ucinewgame')
  94.         self._StateMachine__sendCommand(self._StateMachine__positionCommand)
  95.  
  96.     
  97.     def configure(self, options = []):
  98.         '''
  99.         '''
  100.         if not self._StateMachine__readyToConfigure:
  101.             self._StateMachine__options = options
  102.             return None
  103.         for option in options:
  104.             if option.value == '':
  105.                 continue
  106.             
  107.             self.onOutgoingData('setoption ' + option.name + ' value ' + option.value + '\n')
  108.         
  109.         self.onOutgoingData('isready\n')
  110.  
  111.     
  112.     def requestMove(self, whiteTime, blackTime, ownTime):
  113.         '''
  114.         '''
  115.         if whiteTime == 0:
  116.             whiteTime = 30000
  117.         
  118.         if blackTime == 0:
  119.             blackTime = 30000
  120.         
  121.         self._StateMachine__sendCommand('go wtime %d btime %d' % (whiteTime, blackTime))
  122.  
  123.     
  124.     def undoMove(self):
  125.         '''
  126.         '''
  127.         self._StateMachine__sendCommand('stop')
  128.         (self._StateMachine__positionCommand, _) = self._StateMachine__positionCommand.rsplit(' ', 1)
  129.         if self._StateMachine__positionCommand.endswith(' moves'):
  130.             self._StateMachine__haveMoves = False
  131.             self._StateMachine__positionCommand = 'position startpos'
  132.         
  133.         self._StateMachine__sendCommand(self._StateMachine__positionCommand)
  134.  
  135.     
  136.     def reportMove(self, move, isSelf):
  137.         '''
  138.         '''
  139.         if not self._StateMachine__haveMoves:
  140.             self._StateMachine__positionCommand += ' moves'
  141.         
  142.         self._StateMachine__haveMoves = True
  143.         self._StateMachine__positionCommand += ' ' + move
  144.         self._StateMachine__sendCommand(self._StateMachine__positionCommand)
  145.  
  146.     
  147.     def parseLine(self, line):
  148.         '''
  149.         '''
  150.         words = line.split()
  151.         while True:
  152.             if len(words) == 0:
  153.                 self.logText(line + '\n', 'input')
  154.                 return None
  155.             style = self.parseCommand(words[0], words[1:])
  156.             if style is not None:
  157.                 self.logText(line + '\n', style)
  158.                 return None
  159.             print 'WARNING: Unknown command: ' + repr(words[0])
  160.             words = words[1:]
  161.             continue
  162.             len(words) == 0
  163.  
  164.     
  165.     def parseCommand(self, command, args):
  166.         '''
  167.         '''
  168.         if command == 'id':
  169.             return 'info'
  170.         if command == 'uciok':
  171.             self._StateMachine__readyToConfigure = True
  172.             return 'info'
  173.         if command == 'readyok':
  174.             if len(args) != 0:
  175.                 print 'WARNING: Arguments on readyok: ' + str(args)
  176.             
  177.             self._StateMachine__ready = True
  178.             return 'info'
  179.         if command == 'bestmove':
  180.             if len(args) == 0:
  181.                 print 'WARNING: No move with bestmove'
  182.                 return 'error'
  183.             move = args[0]
  184.             self.onMove(move)
  185.             return 'move'
  186.         command == 'bestmove'
  187.         if command == 'info':
  188.             return 'info'
  189.         if command == 'option':
  190.             return 'info'
  191.  
  192.  
  193.